﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class DataAnalyseController : MonoBehaviour
{
    public TextAsset[] clientLogs;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    [ContextMenu("AnalyseClientLogs")]
    void AnalyseClientLogs()
    {
        string content = "";
        foreach (var textAsset in clientLogs)
        {
            content += textAsset.text;
        }

        List<long> users =new List<long>();
        List<string> ips =new List<string>();
        Dictionary<string,int> clientLogIdsCount =new Dictionary<string, int>();
        Dictionary<string,string> userBehaviours = new Dictionary<string, string>();
        
        string[] clientLogStrs = content.Split('\n');

        foreach (string clientLogLine in clientLogStrs)
        {
            string[] values = GetClientLogValues(clientLogLine);

            string time = values[0];
            string ip = values[1];
            long.TryParse(values[2], out long userId);
            string clientLogId = values[3];
            //ClientLogId clientLogId = (ClientLogId)Enum.Parse(typeof(ClientLogId),values[3]);
            string log = values[4];

            if (userId != 0 && !users.Contains(userId))
            {
                users.Add(userId);
            }
            if (!string.IsNullOrEmpty(ip) && !ips.Contains(ip))
            {
                ips.Add(ip);
            }
            if (clientLogIdsCount.ContainsKey(clientLogId))
            {
                clientLogIdsCount[clientLogId] = clientLogIdsCount[clientLogId] + 1;
            }
            else
            {
                clientLogIdsCount[clientLogId] = 1;
            }

            if (userBehaviours.ContainsKey(ip))
            {
                userBehaviours[ip] += clientLogLine + "\n";
            }
            else
            {
                userBehaviours[ip] = clientLogLine + "\n";
            }
        }
        Debug.Log($"user total count:{users.Count}  ip total count:{ips.Count}");
        
        Debug.Log($"clientLog:{clientLogIdsCount.GetDictStr(true)}");
        
        File.WriteAllText(Application.dataPath+"/0Z5_Test/Scripts/DataAnalyse/Txt/"+userBehaviourFile,userBehaviours.GetDictStr());

    }

    private const string userBehaviourFile = "UserBehaviours.txt";
    

    [ContextMenu("TestGetLineValue")]
    void TestGetLineValue()
    {
        string line = "[Saturday, November 7, 2020 1:56:54 AM] [180.249.65.86] [00124] [EnterHomePanel       ] : time:08.56.55 ";
        GameDebug.Log(GetClientLogValues(line).GetArrStr());
    }

    string[] GetClientLogValues(string line)
    {
        List<string> res = new List<string>();

        //time
        res.Add(GetStringInSquareBrackets(line));
        
        line = line.Substring(line.IndexOf(']')+1);
        //GameDebug.Log($"line 2:{line}");
        //ip
        res.Add(GetStringInSquareBrackets(line));
        
        line = line.Substring(line.IndexOf(']')+1);
        //GameDebug.Log($"line 3:{line}");
        
        //id
        res.Add(GetStringInSquareBrackets(line));
        
        line = line.Substring(line.IndexOf(']')+1);
        //GameDebug.Log($"line 4:{line}");
        
        //clientLogId
        res.Add(GetStringInSquareBrackets(line));
        
        line = line.Substring(line.IndexOf(']')+1);
        //GameDebug.Log($"line 5:{line}");

        int i = line.IndexOf(':');
        if (i >= 0 && line.Length >=1 )
        {
            res.Add(line.Substring(i)+1);
        }
        else
        {
            res.Add("");
        }
        
        return res.ToArray();
    }

    string GetStringInSquareBrackets(string str)
    {
        int begin = str.IndexOf('[')+1;
        int end = str.IndexOf(']');
        
        if (begin < 0 || end < 0) 
            return "";
        
        string res = str.Substring(begin, end - begin);
        return res;
    }
    
    // Update is called once per frame
    void Update()
    {
        
    }
}
